package com.mpl.tictactoe;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	private Button b1;
	private Button b2;
	private Button b3;
	private Button b4;
	private Button b5;
	private Button b6;
	private Button b7;
	private Button b8;
	private Button b9;
	private int player;
	private int[][] grille;
	private int nbCoupJoues;
	boolean isOver;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.activity_main);
        
        player = 1;
        grille = new int[3][3];
        isOver = false;
        
        for(int i=0 ; i < 3 ; i++){
			for(int j=0 ; j < 3 ; j++){
				grille[i][j] = 0;
			}
		}
        
        nbCoupJoues = 0;
        
        b1 = (Button)findViewById(R.id.b1);
        b2 = (Button)findViewById(R.id.b2);
        b3 = (Button)findViewById(R.id.b3);
        b4 = (Button)findViewById(R.id.b4);
        b5 = (Button)findViewById(R.id.b5);
        b6 = (Button)findViewById(R.id.b6);
        b7 = (Button)findViewById(R.id.b7);
        b8 = (Button)findViewById(R.id.b8);
        b9 = (Button)findViewById(R.id.b9);
        
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
        b7.setOnClickListener(this);
        b8.setOnClickListener(this);
        b9.setOnClickListener(this);

        b1.setBackgroundColor(Color.BLACK);
        b2.setBackgroundColor(Color.BLACK);
        b3.setBackgroundColor(Color.BLACK);
        b4.setBackgroundColor(Color.BLACK);
        b5.setBackgroundColor(Color.BLACK);
        b6.setBackgroundColor(Color.BLACK);
        b7.setBackgroundColor(Color.BLACK);
        b8.setBackgroundColor(Color.BLACK);
        b9.setBackgroundColor(Color.BLACK);
    }

	@Override
	public void onClick(View v) {
		nbCoupJoues++;
		
		Button myB = (Button)findViewById(v.getId());
		myB.setEnabled(false);
		if(player == 1)
			myB.setBackgroundDrawable(getResources().getDrawable(R.drawable.cross));
		else
			myB.setBackgroundDrawable(getResources().getDrawable(R.drawable.circle));
				
		if(v.equals(b1)){
			grille[0][0]=player;
		}
		else if(v.equals(b2)){
			grille[0][1]=player;
		}
		else if(v.equals(b3)){
			grille[0][2]=player;
		}
		else if(v.equals(b4)){
			grille[1][0]=player;
		}
		else if(v.equals(b5)){
			grille[1][1]=player;
		}
		else if(v.equals(b6)){
			grille[1][2]=player;
		}
		else if(v.equals(b7)){
			grille[2][0]=player;
		}
		else if(v.equals(b8)){
			grille[2][1]=player;
		}
		else if(v.equals(b9)){
			grille[2][2]=player;
		}
		
		isOver = isFinished(player);
		
		if(isOver || nbCoupJoues == 9){
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setCancelable(false);
			adb.setTitle("Game over");
			
			if(isOver){
				adb.setMessage("Player " + player + " won. Play again?");
			}
			else if(nbCoupJoues == 9){
				adb.setMessage(R.string.draw);
			}
			adb.setIcon(android.R.drawable.ic_dialog_alert);
			adb.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

			    public void onClick(DialogInterface dialog, int whichButton) {
			    	nbCoupJoues = 0;
			    	isOver = false;
			    	b1.setEnabled(true);
					b1.setBackgroundColor(Color.BLACK);
					b2.setEnabled(true);
					b2.setBackgroundColor(Color.BLACK);
					b3.setEnabled(true);
					b3.setBackgroundColor(Color.BLACK);
					b4.setEnabled(true);
					b4.setBackgroundColor(Color.BLACK);
					b5.setEnabled(true);
					b5.setBackgroundColor(Color.BLACK);
					b6.setEnabled(true);
					b6.setBackgroundColor(Color.BLACK);
					b7.setEnabled(true);
					b7.setBackgroundColor(Color.BLACK);
					b8.setEnabled(true);
					b8.setBackgroundColor(Color.BLACK);
					b9.setEnabled(true);
					b9.setBackgroundColor(Color.BLACK);
					
					for(int i=0 ; i < 3 ; i++){
						for(int j=0 ; j < 3 ; j++){
							grille[i][j] = 0;
						}
					}
					
					player = 1;
			    }});
			adb.setNegativeButton(R.string.no_leave, new DialogInterface.OnClickListener() {

				 public void onClick(DialogInterface dialog, int whichButton) {
					 finish();
				 }
			 });
			adb.show();
			
			
			
		}
		else{
			if(player == 1)
				player = 2;
			else
				player = 1;
		}
	}
    
	public boolean isFinished(int _player){
		boolean result = false;
		
		for(int col=0; col < 3; col++){
			if( grille[0][col] == _player &&
				grille[1][col] == _player &&
				grille[2][col] == _player)
				result = true;
		}
		
		for(int ligne=0; ligne < 3; ligne++){
			if( grille[ligne][0] == _player &&
				grille[ligne][1] == _player &&
				grille[ligne][2] == _player)
				result = true;
		}
		
		if( grille[0][0] == _player &&
			grille[1][1] == _player &&
			grille[2][2] == _player){
			result = true;
		}
		
		if( grille[0][2] == _player &&
			grille[1][1] == _player &&
			grille[2][0] == _player){
			result = true;
		}
		return result;
	}
}
